//
//  KFDMessagesViewController.m
//  KFD
//
//  Created by Lucky Luke on 8/14/15.
//  Copyright (c) 2015 kfd. All rights reserved.
//

#import "KFDMessageListViewController.h"
#import "KFDUIConfigurator.h"
#import "KFDClient.h"
#import <MJRefresh/MJRefresh.h>
#import "JTToast.h"
#import "KFDMessagesViewCell.h"
#import "ui_config.h"
#import "KFDMessageViewController.h"

#define KFDMessageViewCellReuseIdentifier @"KFDMessageViewCell"
#define KFDMessageViewCellNibName @"KFDMessageViewCell"

@interface KFDMessageListViewController ()

@property (strong,nonatomic) NSArray* messages;

@end

@implementation KFDMessageListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    KFDUIConfigurator* uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=@"消息提醒";
    self.tableView.backgroundColor=KFD_BG_COLOR;
    UIView* headerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    headerView.backgroundColor=KFD_BG_COLOR;
    UIView* footerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    footerView.backgroundColor=KFD_BG_COLOR;
    self.tableView.tableHeaderView=headerView;
    self.tableView.tableFooterView=footerView;
    KFDClient* client=[KFDClient sharedClient];
    [client getMessagesWithMaxCountPerPage:20 page:1 successHandler:^(NSArray *messages) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.messages=messages;
            [self.tableView reloadData];
        });
    } failureHandler:^(KFDResponseBody *response) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[JTToast makeText:response.message] showInView:self.view size:CGSizeMake(response.message.length*20, 40)];
        });
    }];
}


-(void)setupRefreshing
{
    __weak typeof(self) weakSelf=self;
    KFDClient* client=[KFDClient sharedClient];
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [client getMessagesWithMaxCountPerPage:weakSelf.messages.count page:1 successHandler:^(NSArray *messages) {
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.messages=messages;
                [weakSelf.tableView reloadData];
            });
        } failureHandler:^(KFDResponseBody *response) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [[JTToast makeText:response.message] showInView:weakSelf.view size:CGSizeMake(response.message.length*20, 40)];
            });
        }];
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        [client getMessagesWithMaxCountPerPage:weakSelf.messages.count+10 page:1 successHandler:^(NSArray *messages) {
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.messages=messages;
                [weakSelf.tableView reloadData];
            });
        } failureHandler:^(KFDResponseBody *response) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [[JTToast makeText:response.message] showInView:weakSelf.view size:CGSizeMake(response.message.length*20, 40)];
            });
        }];
    }];
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//------------------------------------------------------------------------------------
#pragma mark - Table view data source
//------------------------------------------------------------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.messages.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KFDMessagesViewCell *cell = [tableView dequeueReusableCellWithIdentifier:KFDMessageViewCellReuseIdentifier];
    if (!cell) {
        cell=[[NSBundle mainBundle] loadNibNamed:KFDMessageViewCellNibName owner:nil options:nil].firstObject;
    }
    cell.message=self.messages[indexPath.row];
    return cell;
}
//------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------
#pragma mark - Table view delegate
//------------------------------------------------------------------------------------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KFDMessageViewController* msgVC=[[KFDMessageViewController alloc] init];
    msgVC.message=self.messages[indexPath.row];
    [self.navigationController pushViewController:msgVC animated:YES];
}
//------------------------------------------------------------------------------------

@end
